﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using Autodesk.Revit.DB;

namespace YJKPresentation.CrossFloor
{
	public class SelectionSetManager
	{
		public List<SelectionSet> SelectionSetList
		{
			get
			{
				return this.m_SelectionSetList;
			}
		}

		public SelectionSetManager(SelectionSetManager other)
		{
			this.m_SelectionSetList = other.m_SelectionSetList;
			this.m_DocumentName = other.m_DocumentName;
			this.m_ViewId = other.m_ViewId;
			this.m_XMLFileName = other.m_XMLFileName;
		}

		public SelectionSetManager(string documentPathName, ElementId viewId)
		{
			this.SetDocumentPathName(documentPathName);
			this.m_ViewId = viewId;
		}

		public bool FileIsNotSaved()
		{
			return !File.Exists(this.m_DocumentName);
		}

		public void SetDocumentPathName(string pathName)
		{
			this.m_DocumentName = pathName;
			this.SetXMLFilePathName();
		}

		private void SetXMLFilePathName()
		{
			int num = this.m_DocumentName.LastIndexOf('.');
			if (num != -1)
			{
				this.m_XMLFileName = this.m_DocumentName.Substring(0, num) + "_HY.xml";
			}
		}

		public void AddSelectionSet(SelectionSet selSet)
		{
			if (this.m_SelectionSetList.Count == 5)
			{
				this.m_SelectionSetList.RemoveAt(0);
			}
			this.m_SelectionSetList.Add(selSet);
		}

		public bool ReadInfoFromFile(Autodesk.Revit.DB.Document doc)
		{
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				xmlDocument.Load(this.m_XMLFileName);
			}
			catch (Exception)
			{
				return false;
			}
			XmlNode xmlNode = xmlDocument.SelectSingleNode("DocumentsList");
			if (xmlNode == null)
			{
				return false;
			}
			XmlNodeList childNodes = xmlNode.ChildNodes;
			XmlNode xmlNode2 = null;
			foreach (object obj in childNodes)
			{
				XmlNode xmlNode3 = (XmlNode)obj;
				if ((xmlNode3 as XmlElement).GetAttribute("Name").CompareTo(this.m_DocumentName) == 0)
				{
					xmlNode2 = xmlNode3;
					break;
				}
			}
			if (xmlNode2 == null)
			{
				return false;
			}
			XmlNodeList childNodes2 = xmlNode2.ChildNodes;
			XmlNode xmlNode4 = null;
			foreach (object obj2 in childNodes2)
			{
				XmlNode xmlNode5 = (XmlNode)obj2;
				int num = Convert.ToInt32((xmlNode5 as XmlElement).GetAttribute("Id"));
				if (this.m_ViewId.IntegerValue == num)
				{
					xmlNode4 = xmlNode5;
					break;
				}
			}
			if (xmlNode4 == null)
			{
				return false;
			}
			foreach (object obj3 in xmlNode4.ChildNodes)
			{
				XmlNode xmlNode6 = (XmlNode)obj3;
				SelectionSet selectionSet = new SelectionSet();
				selectionSet.ReadInfoFromFile(doc, xmlNode6 as XmlElement);
				if (selectionSet.SelectedElementIdList.Count > 0)
				{
					this.m_SelectionSetList.Add(selectionSet);
				}
			}
			return true;
		}

		public void WriteInfoToFile()
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlElement xmlElement = null;
			if (File.Exists(this.m_XMLFileName))
			{
				try
				{
					xmlDocument.Load(this.m_XMLFileName);
				}
				catch (Exception)
				{
					return;
				}
				XmlElement xmlElement2 = xmlDocument.SelectSingleNode("DocumentsList") as XmlElement;
				if (xmlElement2 == null)
				{
					return;
				}
				IEnumerator enumerator = xmlElement2.ChildNodes.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						object obj = enumerator.Current;
						XmlElement xmlElement3 = ((XmlNode)obj) as XmlElement;
						if (xmlElement3 != null && xmlElement3.GetAttribute("Name").CompareTo(this.m_DocumentName) == 0)
						{
							xmlElement = xmlElement3;
							foreach (object obj2 in xmlElement3.ChildNodes)
							{
								XmlNode xmlNode = (XmlNode)obj2;
								int num = Convert.ToInt32((xmlNode as XmlElement).GetAttribute("Id"));
								if (this.m_ViewId.IntegerValue == num)
								{
									xmlElement3.RemoveChild(xmlNode);
									break;
								}
							}
						}
					}
					goto IL_1A4;
				}
			}
			XmlNode xmlNode2 = xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "", "");
			XmlNode xmlNode3 = xmlNode2;
			xmlNode3.InnerText += " encoding=\"GB2312\"";
			xmlDocument.AppendChild(xmlNode2);
			XmlElement xmlElement4 = xmlDocument.CreateElement("", "DocumentsList", "");
			xmlElement = xmlDocument.CreateElement("", "Document", "");
			xmlElement.SetAttribute("Name", this.m_DocumentName);
			xmlElement4.AppendChild(xmlElement);
			xmlDocument.AppendChild(xmlElement4);
			IL_1A4:
			XmlElement xmlElement5 = xmlDocument.CreateElement("", "View", "");
			xmlElement5.SetAttribute("Id", this.m_ViewId.IntegerValue.ToString());
			xmlElement.AppendChild(xmlElement5);
			foreach (SelectionSet selectionSet in this.m_SelectionSetList)
			{
				XmlElement newChild = xmlDocument.CreateElement("SelectionSet");
				selectionSet.WriteInfoToFile(xmlDocument, ref newChild);
				xmlElement5.AppendChild(newChild);
			}
			try
			{
				if (File.Exists(this.m_XMLFileName))
				{
					File.SetAttributes(this.m_XMLFileName, FileAttributes.Normal);
				}
				xmlDocument.Save(this.m_XMLFileName);
				File.SetAttributes(this.m_XMLFileName, FileAttributes.Hidden);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
			}
		}

		private List<SelectionSet> m_SelectionSetList = new List<SelectionSet>();

		private string m_DocumentName = "";

		private ElementId m_ViewId;

		private string m_XMLFileName = "";
	}
}
